import axios from "axios"
import { getToken } from "./auth";

const instance = axios.create({
    baseURL: 'http://localhost:3000',
    timeout: 5000
});

//全局请求拦截，发送请求之前执行
instance.interceptors.request.use(
    function (config) {
        // config.headers['authorization'] = 'Bearer ' + getToken()
        config.headers = { "authorization": 'Bearer ' + getToken(), ...config.headers }
        return config;
    }, function (error) {
        return Promise.reject(error)
    }
);

//请求返回之后执行
instance.interceptors.response.use(
    function (response) {
        return response.data
    },
    function (error) {
        return Promise.reject(error);
    }
);

export function get(url, param) {
    return instance.get(url, { params: param })
}

export function post(url, param) {
    return instance.post(url, param)
}

export function put(url, param) {
    return instance.put(url, param)
}

export function del(url) {
    return instance.del(url)
}